﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.DevicesControls
{
    public partial class FanForm : Form
    {
        public FanForm()
        {
            InitializeComponent();
        }

        Fan fan;
        public FanForm(Fan fancontrol)
        {
            InitializeComponent();
            fan = fancontrol;

            switch (fan.StatusDescription)
            {
                case "正转": r1.Checked = true; r2.Enabled = false; break;
                case "反转": r2.Checked = true; r1.Enabled = false; break;
                case "停止": r3.Checked = true; break;
            }
            name.Text = fan.Name;
            pos2.Text = fan.DevicePos;
            if (!Global.PlcStatus[fan.Protocol])
                OK.Enabled = false;
        }

        private void exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (r1.Checked == true)
            {
                //fan.StatusDescription = "正转";
                bwCommand.RunWorkerAsync("正转");
                this.Close();
                return;
            }
            if (r2.Checked == true)
            {
                //fan.StatusDescription = "反转";
                bwCommand.RunWorkerAsync("反转");
                this.Close();
                return;
            }
            if (r3.Checked == true)
            {
                //fan.StatusDescription = "停止";
                bwCommand.RunWorkerAsync("停止");
                this.Close();
                return;
            }

            MessageBox.Show("请选择风机状态");
        }

        private void FanForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = fan.Zzid + fan.Fzid + fan.Tzid;
        }
        private int fanIntervalTime = Properties.Settings.Default.FanIntervalTime;
        private void bwCommand_DoWork(object sender, DoWorkEventArgs e)
        {
            if (Tools.FanTimeControl.getSeconds() != 0)
            {
                MessageBox.Show("控制风机间隔过短，请于" + Tools.FanTimeControl.getSeconds() + "秒后重试", "请不要操作风机过快", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                return;
            }


            Global g = new Global();
            string CommandID = g.CommandID;
            //string sqlzz1 = "update R_RunStatus set ParamValue='1',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Zzid + "'";
            //string sqlzz2 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Fzid + "'";
            //string sqlzz3 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Tzid + "'";
            //string[] sqlTranCommandsZZ = { sqlzz1, sqlzz2, sqlzz3 };


            //string sqlfz1 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Zzid + "'";
            //string sqlfz2 = "update R_RunStatus set ParamValue='1',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Fzid + "'";
            //string sqlfz3 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Tzid + "'";
            //string[] sqlTranCommandsFZ = { sqlfz1, sqlfz2, sqlfz3 };

            //string sqltz1 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Zzid + "'";
            //string sqltz2 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Fzid + "'";
            //string sqltz3 = "update R_RunStatus set ParamValue='1',UpdateTime=GETDATE(),Runned='0' where RSID='" + fan.Tzid + "'";
            //string[] sqlTranCommandsTZ = { sqltz1, sqltz2, sqltz3 };


            string sqlzz = "INSERT INTO R_Command(CommandID, DeviceID, ParamName, ParamValue, UpdateTime, UpdateBy, Runned) VALUES('" + CommandID + "', '" + fan.DeviceID + "', '" + fan.Zzid + "', '1', GETDATE(), '" + Global.User + "', 0)";
            string sqlfz = "INSERT INTO R_Command(CommandID, DeviceID, ParamName, ParamValue, UpdateTime, UpdateBy, Runned) VALUES('" + CommandID + "', '" + fan.DeviceID + "', '" + fan.Fzid + "', '1', GETDATE(), '" + Global.User + "', 0)";
            string sqltz = "INSERT INTO R_Command(CommandID, DeviceID, ParamName, ParamValue, UpdateTime, UpdateBy, Runned) VALUES('" + CommandID + "', '" + fan.DeviceID + "', '" + fan.Tzid + "', '1', GETDATE(), '" + Global.User + "', 0)";

            string status = e.Argument as string;
            SqlDBHelper SDH = new SqlDBHelper();
            switch (status)
            {
                case "正转": SDH.ExecuteCommand(sqlzz); Tools.FanTimeControl.LastFanTime = DateTime.Now; LogDB.CommandLog("FAN", fan.DeviceID, "开启正转"); break;
                case "反转": SDH.ExecuteCommand(sqlfz); Tools.FanTimeControl.LastFanTime = DateTime.Now; LogDB.CommandLog("FAN", fan.DeviceID, "开启反转"); break;
                case "停止": SDH.ExecuteCommand(sqltz); LogDB.CommandLog("FAN", fan.DeviceID, "停止风机"); break;
            }

        }
    }
}
